﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace PSE.Deployment.BuildManager.Tools
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        private const string PublicKeyToken = "aaaaaaaaaaaaaaaa";

        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ChangePublicKeyToken()
        {
            StringBuilder file = new StringBuilder();
            file.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            file.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696, processorArchitecture=MSIL\">");
            file.AppendLine("<SpecificVersion>False</SpecificVersion>");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");

            StringBuilder expected = new StringBuilder();
            expected.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa, processorArchitecture=MSIL\">");
            expected.AppendLine("  <SpecificVersion>True</SpecificVersion>");
            expected.AppendLine(@"  <HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            expected.AppendLine("  <Private>False</Private>");
            expected.Append("</Reference>");

            Version version = new Version(1, 0, 0, 0);

            XDocument document = XDocument.Parse(file.ToString());
            UpdateProjectReferences updater = new UpdateProjectReferences();
            updater.ChangePseReference(document, version, PublicKeyToken);

            Assert.AreEqual(document.ToString(), expected.ToString());
        }

        [TestMethod]
        public void NonPseFrameworkAssembly()
        {
            StringBuilder file = new StringBuilder();
            file.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            file.AppendLine("<Reference Include=\"PSE.Deployment.BuildManager.ExternalProcess.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL\">");
            file.AppendLine("<SpecificVersion>False</SpecificVersion>");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");

            StringBuilder expected = new StringBuilder();
            expected.AppendLine("<Reference Include=\"PSE.Deployment.BuildManager.ExternalProcess.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL\">");
            expected.AppendLine("  <SpecificVersion>False</SpecificVersion>");
            expected.AppendLine(@"  <HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            expected.AppendLine("  <Private>False</Private>");
            expected.Append("</Reference>");

            Version version = new Version(3, 0, 0, 0);

            XDocument document = XDocument.Parse(file.ToString());
            UpdateProjectReferences updater = new UpdateProjectReferences();
            updater.ChangePseReference(document, version, PublicKeyToken);

            Assert.AreEqual(expected.ToString(), document.ToString());

        }

        [TestMethod]
        public void CreatePublicKeyToken()
        {
            StringBuilder file = new StringBuilder();
            file.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            file.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL\">");
            file.AppendLine("<SpecificVersion>False</SpecificVersion>");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");

            StringBuilder expected = new StringBuilder();
            expected.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa, processorArchitecture=MSIL\">");
            expected.AppendLine("  <SpecificVersion>True</SpecificVersion>");
            expected.AppendLine(@"  <HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            expected.AppendLine("  <Private>False</Private>");
            expected.Append("</Reference>");

            Version version = new Version(1, 0, 0, 0);

            XDocument document = XDocument.Parse(file.ToString());
            UpdateProjectReferences updater = new UpdateProjectReferences();
            updater.ChangePseReference(document, version, PublicKeyToken);

            Assert.AreEqual(expected.ToString(), document.ToString());
        }

        [TestMethod]
        public void ChangeVersionFromSimpleFile()
        {
            StringBuilder file = new StringBuilder();
            file.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            file.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=" + PublicKeyToken + ", processorArchitecture=MSIL\">");
            file.AppendLine("<SpecificVersion>False</SpecificVersion>");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");

            StringBuilder expected = new StringBuilder();
            expected.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=" + PublicKeyToken + ", processorArchitecture=MSIL\">");
            expected.AppendLine("  <SpecificVersion>True</SpecificVersion>");
            expected.AppendLine(@"  <HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            expected.AppendLine("  <Private>False</Private>");
            expected.Append("</Reference>");

            Version version = new Version(3, 0, 0, 0);

            XDocument document = XDocument.Parse(file.ToString());
            UpdateProjectReferences updater = new UpdateProjectReferences();
            updater.ChangePseReference(document, version, PublicKeyToken);
            
            Assert.AreEqual(document.ToString(), expected.ToString());
        }
        
        [TestMethod]
        public void CreateSpecifVersionElement()
        {
            StringBuilder file = new StringBuilder();
            file.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            file.AppendLine("<Project ToolsVersion=\"3.5\" DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">");
            file.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696, processorArchitecture=MSIL\">");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");
            file.Append("</Project>");

            StringBuilder expected = new StringBuilder();
            expected.AppendLine("<Project ToolsVersion=\"3.5\" DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">");
            expected.AppendLine("  <Reference Include=\"PSE.Framework.UI.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=" + PublicKeyToken + ", processorArchitecture=MSIL\">");
            expected.AppendLine(@"    <HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            expected.AppendLine("    <Private>False</Private>");
            expected.AppendLine("    <SpecificVersion>True</SpecificVersion>");
            expected.AppendLine("  </Reference>");
            expected.Append("</Project>");

            Version version = new Version(3, 0, 0, 0);

            XDocument document = XDocument.Parse(file.ToString());
            UpdateProjectReferences updater = new UpdateProjectReferences();
            updater.ChangePseReference(document, version, PublicKeyToken);

            Assert.AreEqual(document.ToString(), expected.ToString());
        }

        public void ChangeVersionFromFullFile()
        {
            //
            // TODO: Add test logic	here
            //
            StringBuilder file = new StringBuilder();
            file.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            file.AppendLine("<Project ToolsVersion=\"3.5\" DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">");
            file.AppendLine("<PropertyGroup>");
            file.AppendLine("<Configuration Condition=\" '$(Configuration)' == '' \">Debug</Configuration>");
            file.AppendLine("<Platform Condition=\" '$(Platform)' == '' \">AnyCPU</Platform>");
            file.AppendLine("<ProductVersion>9.0.21022</ProductVersion>");
            file.AppendLine("<SchemaVersion>2.0</SchemaVersion>");
            file.AppendLine("<ProjectGuid>{5927C137-5366-42A2-9634-08797A9E80BE}</ProjectGuid>");
            file.AppendLine("<OutputType>Library</OutputType>");
            file.AppendLine("<AppDesignerFolder>Properties</AppDesignerFolder>");
            file.AppendLine("<RootNamespace>IMercado.Framework.UI.Logging</RootNamespace>");
            file.AppendLine("<AssemblyName>IMercado.Framework.UI.Logging</AssemblyName>");
            file.AppendLine("<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>");
            file.AppendLine("<FileAlignment>512</FileAlignment>");
            file.AppendLine("<SccProjectName>SAK</SccProjectName>");
            file.AppendLine("<SccLocalPath>SAK</SccLocalPath>");
            file.AppendLine("<SccAuxPath>SAK</SccAuxPath>");
            file.AppendLine("<SccProvider>SAK</SccProvider>");
            file.AppendLine("</PropertyGroup>");
            file.AppendLine("<ItemGroup>");
            file.AppendLine("<Reference Include=\"PSE.Framework.Common.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696, processorArchitecture=MSIL\">");
            file.AppendLine("<SpecificVersion>False</SpecificVersion>");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Common\PSE.Framework.Common.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");
            file.AppendLine("<Reference Include=\"PSE.Framework.UI.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696, processorArchitecture=MSIL\">");
            file.AppendLine("<SpecificVersion>False</SpecificVersion>");
            file.AppendLine(@"<HintPath>\DSEPROJ\PSE\Compilados\Framework\Interface Layer\PSE.Framework.UI.Logging.dll</HintPath>");
            file.AppendLine("<Private>False</Private>");
            file.AppendLine("</Reference>");
            file.AppendLine("<Reference Include=\"System\" />");
            file.AppendLine("<Reference Include=\"System.Core\">");
            file.AppendLine("<RequiredTargetFramework>3.5</RequiredTargetFramework>");
            file.AppendLine("</Reference>");
            file.AppendLine("<Reference Include=\"System.Xml.Linq\">");
            file.AppendLine("<RequiredTargetFramework>3.5</RequiredTargetFramework>");
            file.AppendLine("</Reference>");
            file.AppendLine("<Reference Include=\"System.Data.DataSetExtensions\">");
            file.AppendLine("<RequiredTargetFramework>3.5</RequiredTargetFramework>");
            file.AppendLine("</Reference>");
            file.AppendLine("<Reference Include=\"System.Data\" />");
            file.AppendLine("<Reference Include=\"System.Xml\" />");
            file.AppendLine("</ItemGroup>");
            file.AppendLine("<ItemGroup>");
            file.AppendLine("<Compile Include=\"ExceptionLog.cs\" />");
            file.AppendLine("<Compile Include=\"MessageLog.cs\" />");
            file.AppendLine("<Compile Include=\"Properties\\AssemblyInfo.cs\" />");
            file.AppendLine("</ItemGroup>");
            file.AppendLine("<Import Project=\"$(MSBuildToolsPath)\\Microsoft.CSharp.targets\" />");
            file.AppendLine("<!-- To modify your build process, add your task inside one of the targets below and uncomment it."); 
            file.AppendLine("Other similar extension points exist, see Microsoft.Common.targets.");
            file.AppendLine("<Target Name=\"BeforeBuild\">");
            file.AppendLine("</Target>");
            file.AppendLine("<Target Name=\"AfterBuild\">");
            file.AppendLine("</Target>");
            file.AppendLine("-->");
            file.AppendLine("</Project>");

            
        }
    }
}
